void CreateNDate()
{
	// 造数据
	int n = 10000;
	srand(time(0));
	const char* file = "data.txt";
	FILE* fin = fopen(file, "w");
	if (fin == NULL)
	{
		perror("fopen error");
		return;
	}

	for (size_t i = 0; i < n; ++i)
	{
		int x = rand() % 1000000;
		fprintf(fin, "%d\n", x);
	}

	fclose(fin);
}


//建小堆
void AdjustDown(HPDataType* a, int parent, int n)
{
	assert(a);
	int child = parent * 2 + 1;
	while (child<n)
	{
		//找左 右孩子大的一个
		if (child+1<n &&a[child] > a[child + 1])
		{
			child++;
		}
		if (a[parent] > a[child])
		{
			Sweap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
		
	}
}



void PrintTopK(int k)
{
	const char* file = "data.txt";
	FILE* fout = fopen(file, "r");

	if (fout == NULL)
	{
		perror("fopen fail\n");
		return;
	}

	HPDataType* KminHeap = (HPDataType*)malloc(sizeof(HPDataType) * k);
	if (KminHeap == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	for (int i = 0; i < k; i++)
	{
		fscanf(fout, "%d", &KminHeap[i]);

	}

	//建小堆
	for (int i = (k - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(KminHeap, i, k);
	}

	int val = 0;
	while (!feof(fout))
	{
		fscanf(fout, "%d", &val);
		if (val>KminHeap[0])
		{
			KminHeap[0] = val;
			AdjustDown(KminHeap,0,k);

		}

	}

	for (int i = 0; i < k; i++)
	{
		printf("%d ", KminHeap[i]);
	}
}





int main()
{
	CreateNDate();
	PrintTopK(5);

	return 0;
}